{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>优惠券编辑</legend>
    </fieldset>
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">优惠券名称</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="title" value="" lay-verify="required"
                    placeholder="请输入优惠券名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-inline">
                <select name="cate_id" lay-filter="interest-search" lay-verify="cate_id" lay-search placeholder="请选择优惠券分类">
                    {volist name="list_category" id="vo"}
                    <option value="{$vo.id}" >{$vo.title}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">优惠金额</label>
            <div class="layui-input-4">
                <input type="number" maxlength="200" name="money" value="" lay-verify="required"
                    placeholder="减多少" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">优惠券优惠金额</div>
            <!-- 优惠券 -->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">优惠门槛</label>
            <div class="layui-input-4">
                <input type="number" maxlength=" 200" name="full_money" value=""
                    lay-verify="required" placeholder="满多少可用" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">优惠券门槛，满多少可用，至少填写优惠券优惠金额相同的金额</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="start_time" value="" id="start_time" lay-verify="required"
                    placeholder="优惠券开始使用的时间" class="layui-input" autocomplete="off">
            </div>
            <div class="layui-form-mid layui-word-aux">优惠券开始生效时间，为0则为用户领取时间开始计算</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">有效期</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="end_time" value="" lay-verify="required"
                    placeholder="优惠券有效期" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">优惠券有效时间,单位（天）</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">使用规则</label>
            <div class="layui-input-4" style="width:80%;height:350px;">
                <script id="use_rule" name="use_rule" lay-verify="required" type="text/plain"
                    style="width:80%; height:200px;"></script>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
           <div class="layui-input-4">
            <input type="radio" name="status" value="0" title="正常" checked>
			  <input type="radio" name="status" value="1" title="下架">
           </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发放数量</label>
            <div class="layui-input-4">
                <input type="number" maxlength=" 200" name="number" value=""
                    lay-verify="required" placeholder="请输入发放数量" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发放方式</label>
           <div class="layui-input-4">
            <input type="radio" name="automatic"  value="0" title="手动" checked>
			  <input type="radio" name="automatic" value="1" title="自动">
           </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">活动券</label>
           <div class="layui-input-4">
            <input type="radio" name="is_activity" value="0" title="否" checked>
			  <input type="radio" name="is_activity" value="1" title="是">
           </div>
        </div>
        <div class="layui-form-item" style="display:none">
            <label class="layui-form-label">优惠券价格</label>
            <div class="layui-input-4">
                <input type="number" maxlength=" 200" name="full_money" value="0"
                    lay-verify="required" placeholder="优惠券购买的价格" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submit" lay-submit=""
                    lay-filter="sys">{:lang('submit')}</button>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>
    var ue = UE.getEditor('use_rule');
    ue.ready(function(){
        ue.addListener("contentChange", function () {
	    $('#use_rule').val(ue.getContent());
    });
    })
    layui.use(['form', 'layer', 'upload', 'laydate'], function () {
        var form = layui.form, layer = layui.layer, upload = layui.upload, $ = layui.jquery;
        let laydate = layui.laydate;
        form.verify({
            title: function (value) {
                if (value == '') {
                    return '请输入网站名称！';
                }
            },
            cate_id: function (value) {
                if (value == '') {
                    return '请选择优惠券分类！';
                }
            },
        });
        let start_time = laydate.render({
            elem:'#start_time',
            type:'datetime',
            format:'yyyy-MM-dd HH:mm:ss'
        })
        //普通图片上传    后台登录页背景图片
        var uploadInst2 = upload.render({
            elem: '#logoBtn2'
            , url: '{:url("UpFiles/upload")}'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#SLogo2').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                if (res.errorCode == 8888) {
                    $('#logo2').val(res.url);
                    layer.msg(res.msg, { time: 1000, icon: 1 });
                } else {
                    $('#SLogo2').attr('src', '');
                    layer.msg(res.msg, { time: 2000, icon: 2 });
                }
            }
        });
        //提交监听
        form.on('submit(sys)', function (data) {
            $("#submit").attr({ disabled: "disabled" });
            loading = layer.load(1, { shade: [0.1, '#fff'] });
            let user = data.field
            let time = data.field.end_time
            
            if(time==null || !time){
                data.field.end_time = 0
            }
            if(!data.field.start_time){
                data.field.start_time = 0
            }else{
                data.field.start_time = new Date(data.field.start_time)/1000
            }
            $.post("{:url('add')}", data.field, function (res) {
                layer.close(loading);
                if (res.errorCode == 8888) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        location.href = res.url;
                    });
                } else {
                    $("#submit").removeAttr("disabled");
                    layer.msg(res.msg, { icon: 2, time: 1000 });
                }
            });
        })
    })


</script>


</body>

</html>